/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-07-11     
 */
package com.task.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.task.bean.Constants;
import com.task.bean.EnumConstants;
import com.task.bean.EnumConstants.ChildTaskStatus;
import com.task.bean.EnumConstants.ParentTaskStatus;
import com.task.entity.ChildTask;
import com.task.entity.FinishTasks;
import com.task.entity.ParentTask;
import com.task.entity.User;
import com.task.service.IChildTaskService;
import com.task.service.IFinishTasksService;
import com.task.service.IParentTaskService;
import com.task.service.IUserService;
import com.task.utils.RenderUtil;

/**
 * 子任务控制器
 */
@Controller
@RequestMapping("/child")
public class ChildTaskController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ChildTaskController.class);

	@Autowired
	IChildTaskService childTaskService;

	@Autowired
	IParentTaskService parentTaskService;

	@Autowired
	IFinishTasksService finishTaskService;

	@Autowired
	IUserService userService;

	@RequestMapping(value = "/saveChildTask.json")
	public void saveChildTask(HttpServletResponse response) {
		try {
			RenderUtil.renderText(Constants.SUCCESS, response);
		} catch (Exception e) {
			LOGGER.error("saveChildTask error : {}", e);
			RenderUtil.renderText(Constants.FAIL, response);
		}
	}

	@RequestMapping(value = "/subTaskDetail.html")
	public String subTaskDetail(ModelMap model, @RequestParam(value = "id") Long id) {

		ChildTask child = childTaskService.findById(id);
		if (null != child) {
			ParentTask pt = parentTaskService.findById(child.getParentTaskId());
			child.setParentTask(pt);
			model.addAttribute("taskType", pt.getTaskType().getMsg());
			if (StringUtils.isNotBlank(child.getUserId())) {
				User user = userService.findByUserId(child.getUserId());
				if (null != user) {
					model.addAttribute("userName", user.getName());
				}
			}
			FinishTasks finish = finishTaskService.findByChildTaskId(id);
			if (null != finish) {
				if (StringUtils.isNotBlank(finish.getImagesUrl())) {
					String[] urls = finish.getImagesUrl().replaceAll("\\\\","/").split(",");
					model.addAttribute("imageUrls", Arrays.asList(urls));
				}
				model.addAttribute("content",finish.getTaskDesc());
			}
		}
		model.addAttribute("child", child);
		model.addAttribute("status", child.getStatus().getMsg());
		return "/task/subTaskDetail";
	}

	/**
	 * <更新子任务审核状态>
	 * 
	 * @param parentTask
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value = "/checkChildTask.json")
	public String updateChildTask(@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "status", defaultValue = "PASS") EnumConstants.ChildTaskStatus status) {
		// JSONObject resultJson = new JSONObject();
		// resultJson.put(Constants.RESULT, Constants.RESULT_FAIL);
		try {
			childTaskService.updateWhileCheck(status, id);
		} catch (Exception e) {
			LOGGER.error("updateChildTask error {}", e);
			// resultJson.put(Constants.RESULT, Constants.RESULT_FAIL);
			// resultJson.put(Constants.MSG, "修改任务失败，系统错误");
		}
		return "redirect:/index.html";
	}
}
